package org.jeecg.modules.rental.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.config.TenantContext;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.rental.constant.RentalCacheConstant;
import org.jeecg.modules.rental.entity.RentalConfig;
import org.jeecg.modules.rental.enums.RentalConfigEnums;
import org.jeecg.modules.rental.mapper.RentalConfigMapper;
import org.jeecg.modules.rental.service.IRentalConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 租车配置
 * @Author: jeecg-boot
 * @Date:   2024-06-20
 * @Version: V1.0
 */
@Service
public class RentalConfigServiceImpl extends ServiceImpl<RentalConfigMapper, RentalConfig> implements IRentalConfigService {

    @Autowired
    private CacheManager cacheManager;

    @Override
    public void init() {
        List<RentalConfig> configList = this.list();
        List<String> configKeyList = configList.stream().map(RentalConfig::getConfigKey).collect(Collectors.toList());
        List<RentalConfig> configList2 = new ArrayList<>();
        for(RentalConfigEnums item: RentalConfigEnums.values()){
            String configKey = configKeyList.stream().filter(a-> a.equals(item.getConfigKey())).findFirst().orElse(null);
            if(null == configKey){
                RentalConfig rentalConfig = new RentalConfig();
                rentalConfig.setConfigKey(item.getConfigKey());
                rentalConfig.setConfigName(item.getConfigName());
                rentalConfig.setConfigValue(item.getConfigValue());
                rentalConfig.setRemark(item.getRemark());
                configList2.add(rentalConfig);
            }
        }
        this.saveBatch(configList2);
    }

    @Override
    public void initCache() {
        List<RentalConfig> configList = this.list();
        Cache cache = cacheManager.getCache(RentalCacheConstant.RENTAL_CONFIG_CACHE);
        for(RentalConfig item: configList){
            String key = TenantContext.getTenant() + ":" + item.getConfigKey();
            cache.put(key, item);
        }
    }

    @Override
    public RentalConfig get(String configKey) {
        Cache cache = cacheManager.getCache(RentalCacheConstant.RENTAL_CONFIG_CACHE);
        String key = TenantContext.getTenant() + ":" + configKey;
        RentalConfig config = cache.get(key, RentalConfig.class);
        if(oConvertUtils.isEmpty(config)){
            config = this.getOne(new QueryWrapper<RentalConfig>()
                    .eq("config_key",configKey));
            if(oConvertUtils.isNotEmpty(config)){
                cache.put(key, config);
            }
        }
        return config;
    }


    @Override
    public RentalConfig add(RentalConfig entity) {
        if(this.saveOrUpdate(entity)){
            Cache cache = cacheManager.getCache(RentalCacheConstant.RENTAL_CONFIG_CACHE);
            String key = TenantContext.getTenant() + ":" + entity.getConfigKey();
            cache.put(key, entity);
        }
        return entity;
    }

    @Override
    public RentalConfig edit(RentalConfig entity) {
        if(this.saveOrUpdate(entity)){
            Cache cache = cacheManager.getCache(RentalCacheConstant.RENTAL_CONFIG_CACHE);
            String key = TenantContext.getTenant() + ":" + entity.getConfigKey();
            cache.put(key, entity);
        }
        return entity;
    }

    @Override
    public Boolean deleteById(String id) {
        RentalConfig entity = this.getById(id);
        String key = TenantContext.getTenant() + ":" + entity.getConfigKey();
        Cache cache = cacheManager.getCache(RentalCacheConstant.RENTAL_CONFIG_CACHE);
        cache.evict(key); // 删除缓存项
        return this.removeById(id);
    }

    @Override
    public Boolean deleteBatch(List<String> ids) {
        List<RentalConfig> rentalConfigList = this.listByIds(ids);
        Cache cache = cacheManager.getCache(RentalCacheConstant.RENTAL_CONFIG_CACHE);
        if (cache != null) {
            rentalConfigList.forEach(item->{
                String key = TenantContext.getTenant() + ":" + item.getConfigKey();
                cache.evict(key);
            });
        }
        return this.removeByIds(ids);
    }

    @Override
    public List<RentalConfig> batchConfig(List<String> configKeys) {
        return this.list(new QueryWrapper<RentalConfig>()
                .in("config_key",configKeys));
    }

    @Override
    public RentalConfig getVehicleQrCodePrefix() {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        RentalConfig rentalConfig = this.getOne(new QueryWrapper<RentalConfig>()
                .eq("config_key", RentalConfigEnums.VEHICLE_QRCODE_PREFIX.getConfigKey())
                .eq("is_delete", 0)
                .eq("sys_org_code",loginUser.getOrgCode()));
        return rentalConfig;
    }

    @Override
    public RentalConfig getSocketQrCodePrefix() {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        RentalConfig rentalConfig = this.getOne(new QueryWrapper<RentalConfig>()
                .eq("config_key", RentalConfigEnums.CHARGE_QRCODE_PREFIX.getConfigKey())
                .eq("is_delete", 0)
                .eq("sys_org_code",loginUser.getOrgCode()));
        return rentalConfig;
    }


}
